// pages/modify/modify.js
Page({
  data: {
    id: null,
    title: '',
    content: ''
  },

  onLoad(options) {
    this.setData({
      id: options.id,
      title: options.title,
      content: options.content
    });
  },

  onTitleInput(e) {
    this.setData({ title: e.detail.value });
  },

  onContentInput(e) {
    this.setData({ content: e.detail.value });
  },

  saveNote() {
    const notes = wx.getStorageSync('notes') || [];
    const existingNoteIndex = notes.findIndex(note => note.id === this.data.id);
    notes[existingNoteIndex].title = this.data.title;
    notes[existingNoteIndex].content = this.data.content;
    wx.setStorageSync('notes', notes);
    this.refreshApp();
  },

  cancelNote() {
    wx.navigateBack();
  },

  refreshApp() {
    wx.switchTab({
      url: '/pages/note/note'
    });
  }
});
